(function () {
  $(document).on('wechatsdk-ready', function (event, wx) {
    _wechatshareinit(wx);
  });

  function _wechatshareinit(wx) {
    // 如果不是文章页面就不执行
    if (!env.article) return;
    const _article = env.article;
    if (_article.atomStage !== 1 || util.user.op.anonymous) return;
    //
    util
      .performAction({
        method: 'post',
        url: '/a/share/share/generate',
        body: {
          host: 'wechat',
          atomId: _article.atomId,
          url: util.url(_article.url),
        },
      })
      .then(res => {
        const link = res.link;
        _wechatshareinit_share({ wx, _article, link });
      });
  }

  function _wechatshareinit_share({ wx, _article, link }) {
    const title = _article.atomName;
    const desc = _article.description || _article.summary || '';
    const imgUrl = _article.imageCover || _article.imageFirst || '';
    // 分享给朋友
    wx.updateAppMessageShareData({
      title,
      desc,
      link,
      imgUrl,
      success() {
        // 设置成功
      },
    });
    // 分享给朋友圈
    wx.updateTimelineShareData({
      title,
      link,
      imgUrl,
      success() {
        // 设置成功
      },
    });
  }
})();
